$(function() {
	//置顶按钮
	$.scrollUp();
	$(document)
		.on('click', '#_downloadOrBuy', function() {
			var $curbtn = $(this);
			var reportId = $('#_title').attr('report_id');
			var postdata = {
				id: reportId
			};
			$.post('/report/ajaxDownload.html', postdata, function(data) {
				//console.log(data);return;
				switch(data.status) {
					case 200: //已登录
						switch(data.data) {
							case 100: //免费下载
								//TODO 
								//ajaxDownload('/download/fetch.html?id=' + reportId);
								formSubmit({
									url: '/download/fetch.html',
									data: postdata,
									type: 'post'
								});
								break;
							case 200: //已购买
								formSubmit({
									url: '/download/fetch.html',
									data: postdata,
									type: 'post'
								});
								break;
							case 300: //购买
								formSubmit({
									url: '/payreport/info.html',
									data: postdata,
									type: 'post'
								});
								break;
							default:
								toastr.error(data.msg, null, opts)
								break;
						}
						break;
					case 4040: //未登录--引导注册
						if($curbtn.attr('btntype') == 0) { //免费的
							$('._loginBox ._loginText').html('免费注册<br/>免费使用');
							$('._loginBox').modal('show');
						} else { //收费的
							$('._loginBox ._loginText').html('使用服务<br/>只需十秒');
							$('._loginBox').modal('show');
						}
						break;
					default:
						//console.log(data)
						toastr.error(data.msg, null, opts)
						break;
				}
			})
		})
		//立即下载（引导注册弹框）
		//显示报告简介
		$('#_report_desc').html($('#_report_desc_hidden').val());
});

function formSubmit(data) {
	var formInnHTML = ['<form style="display:none" action="' + (data.url || '#') + '" method="' + (data.type || 'post') + '">']
	for(var k in data.data) {
		formInnHTML.push('<input type="text" name="' + k + '" value="' + data.data[k] + '" />');
	}
	formInnHTML.push('<input type="submit" /></form>');
	var submitFrom = $(formInnHTML.join(''))[0];
	//	console.log(submitFrom);
	$('body').append(submitFrom);
	submitFrom.submit();
}